package cheqex.update;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.sql.Date;
import javax.swing.SpinnerNumberModel;

import cheqex.update.controllers.*;

public class DateView extends JFrame implements WindowListener{

	private CheqexFrame cheqexFrame;
	
	private JButton saveDateButton;
	private JButton button;
	private JPanel gdPanel;
	
	private SpinnerNumberModel monthModel;
	private SpinnerNumberModel yearModel;
	private SpinnerNumberModel dateModel;
	
	private JSpinner spinner1;
	private JSpinner spinner2;
	private JSpinner spinner3;
	
	private JLabel m; 
	private JLabel d;
	private JLabel y;
	
	public String date;
	private Calendar calendar;
	
	public DateView(CheqexFrame cheqexFrame){
		this.cheqexFrame = cheqexFrame;
		initialize(cheqexFrame);
	}
	
	//enables buttons
	private void enableButtons(){
		this.cheqexFrame.dailyCollection.setEnabled(true);
		this.cheqexFrame.cashInBank.setEnabled(true);
		this.cheqexFrame.accountSettings.setEnabled(true);
	}
	
	private void disableButtons(){
		this.cheqexFrame.dailyCollection.setEnabled(false);
		this.cheqexFrame.cashInBank.setEnabled(false);
		this.cheqexFrame.accountSettings.setEnabled(false); 
	}
	
	//adds panel to the rightPanel
	private void addPanel(){
		this.cheqexFrame.rightPanel.add(new UpdateDailyView());
		this.cheqexFrame.rightPanel.setVisible(true);
		this.cheqexFrame.pack();
	}
	
	//closes login frame
	private void closeframe(){
		this.setVisible(false);
	}
	
	
	//initializes components
	private void initialize(CheqexFrame cheqexFrame){
		
		this.cheqexFrame.dailyCollection.setEnabled(false);
		this.cheqexFrame.cashInBank.setEnabled(false);
		this.cheqexFrame.accountSettings.setEnabled(false); 
        calendar = Calendar.getInstance();

        //Add the first label-spinner pair.
        int cmonth = calendar.get(Calendar.MONTH)+1;
        monthModel = new SpinnerNumberModel(cmonth, calendar.JANUARY + 1, 
            							calendar.DECEMBER + 1, 1);
        m = new JLabel("Month: ");
        m.setLabelFor(spinner1);
        
        int cday = calendar.get(Calendar.DAY_OF_MONTH);
        dateModel = new SpinnerNumberModel(cday, 1, 31,1);
        d = new JLabel("Day: ");
        d.setLabelFor(spinner2);
        
        int currentYear = calendar.get(Calendar.YEAR);
        yearModel = new SpinnerNumberModel(currentYear, //initial value
                                       currentYear - 100, //min
                                       currentYear + 100, //max
                                       1);                //step
        y = new JLabel("Year: ");
        y.setLabelFor(spinner3);
        
        saveDateButton = new JButton("Next");
        saveDateButton.setMnemonic(KeyEvent.VK_D);
        saveDateButton.setActionCommand("saveDate");
	    saveDateButton.setEnabled(true);	    
	    saveDateButton.addActionListener(new ActionListener(){
	    	@Override
			public void actionPerformed(ActionEvent e) {
			 try{
				if ("saveDate".equals(e.getActionCommand())) {
					getDate();
				}
			 }catch (Exception e1){ e1.printStackTrace();}
			 
			}
	    });
	    
	    gdPanel = new JPanel();
	    gdPanel.add(m);
	    spinner1 = new JSpinner(monthModel);

	    gdPanel.add(spinner1);
	    gdPanel.add(y);
	    spinner2 = new JSpinner(yearModel);
	    gdPanel.add(spinner2);
	    gdPanel.add(d);

	    spinner3 = new JSpinner(dateModel);
	    	    gdPanel.add(spinner3);
        gdPanel.add(saveDateButton);
        
        this.add(gdPanel);
        this.pack();
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        
        addWindowListener(this);
	}
	
	private void getDate(){
		int yyyy = yearModel.getNumber().intValue(); 
		int mm = monthModel.getNumber().intValue();
		int dd = dateModel.getNumber().intValue();
		
		try{
			date = DateController.saveDate(yyyy,mm,dd);
			closeframe();
			enableButtons();
			
			if(!date.equals(null))
				addPanel();
		} catch (Exception arg0) {}
	}

	public JFrame getCheqexFrame() {
		return cheqexFrame;
	}

	public void setCheqexFrame(CheqexFrame cheqexFrame) {
		this.cheqexFrame = cheqexFrame;
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		disableButtons();
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		enableButtons();
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		enableButtons();
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		disableButtons();
	}
	
	
	
}
